/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "zobjects_gtk.h"

ZlangInvokeFunction ZlangInvokeFunction_guiclipboard_CopyText_string;
int ZlangInvokeFunction_guiclipboard_CopyText_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	char						*text = NULL ;
	GdkDisplay					*display = NULL ;
	GdkClipboard					*clipboard = NULL ;

	CallRuntimeFunction_string_GetStringValue( rt , in1 , & text , NULL );

	display = gdk_display_get_default() ;
	clipboard = gdk_display_get_clipboard( display ) ;
	gdk_clipboard_set_text( clipboard , text ) ;

	return 0;
}

static char *g_clipboard_text = NULL ;
static gboolean g_operation_done = FALSE ;

static void on_clipboard_text_ready( GObject *source_object , GAsyncResult *res , gpointer user_data )
{
	GdkClipboard *clipboard = GDK_CLIPBOARD(source_object) ;
	g_clipboard_text = gdk_clipboard_read_text_finish(clipboard, res, NULL) ;
	g_operation_done = TRUE ;
	return;
}

static char *gtk_clipboard_wait_for_text( GdkClipboard *clipboard )
{
	g_operation_done = FALSE;
	g_clipboard_text = NULL ;
	gdk_clipboard_read_text_async( clipboard , NULL , on_clipboard_text_ready , NULL );
	while( ! g_operation_done )
	{
		while( g_main_context_iteration(NULL, FALSE) )
			g_usleep(10);
	}

	return g_clipboard_text;
}

ZlangInvokeFunction ZlangInvokeFunction_guiclipboard_PasteText;
int ZlangInvokeFunction_guiclipboard_PasteText( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	char						*text = NULL ;
	GdkDisplay					*display = NULL ;
	GdkClipboard					*clipboard = NULL ;

	display = gdk_display_get_default() ;
	clipboard = gdk_display_get_clipboard( display ) ;
	text = gtk_clipboard_wait_for_text( clipboard ) ;
	if( text )
	{
		CallRuntimeFunction_string_SetStringValue( rt , out1 , text , STRLEN_OF_STRING );
	}
	else
	{
		UnreferObject( rt , out1 );
	}

	return 0;
}

ZlangCreateDirectPropertyFunction ZlangCreateDirectProperty_guiclipboard;
void *ZlangCreateDirectProperty_guiclipboard( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guiclipboard		*guiclipboard_prop = NULL ;
	
	guiclipboard_prop = (struct ZlangDirectProperty_guiclipboard *)ZLMALLOC( sizeof(struct ZlangDirectProperty_guiclipboard) ) ;
	if( guiclipboard_prop == NULL )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_ALLOC , "alloc memory for entity" )
		return NULL;
	}
	memset( guiclipboard_prop , 0x00 , sizeof(struct ZlangDirectProperty_guiclipboard) );

	return guiclipboard_prop;
}

ZlangDestroyDirectPropertyFunction ZlangDestroyDirectProperty_guiclipboard;
void ZlangDestroyDirectProperty_guiclipboard( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guiclipboard	*guiclipboard_direct_prop = GetObjectDirectProperty(obj) ;
	
	ZLFREE( guiclipboard_direct_prop );
	
	return;
}

ZlangSummarizeDirectPropertySizeFunction ZlangSummarizeDirectPropertySize_guiclipboard;
void ZlangSummarizeDirectPropertySize_guiclipboard( struct ZlangRuntime *rt , struct ZlangObject *obj , size_t *summarized_obj_size , size_t *summarized_direct_prop_size )
{
	SUMMARIZE_SIZE( summarized_direct_prop_size , sizeof(struct ZlangDirectProperty_guiclipboard) )
	return;
}

static struct ZlangDirectFunctions direct_funcs_guiclipboard =
	{
		ZLANG_OBJECT_guiclipboard , /* char *ancestor_name */
		
		ZlangCreateDirectProperty_guiclipboard , /* ZlangCreateDirectPropertyFunction *create_entity_func */
		ZlangDestroyDirectProperty_guiclipboard , /* ZlangDestroyDirectPropertyFunction *destroy_entity_func */
		
		NULL , /* ZlangFromCharPtrFunction *from_char_ptr_func */
		NULL , /* ZlangToStringFunction *to_string_func */
		NULL , /* ZlangFromDataPtrFunction *from_data_ptr_func */
		NULL , /* ZlangGetDataPtrFunction *get_data_ptr_func */
		
		NULL , /* ZlangOperatorFunction *oper_PLUS_func */
		NULL , /* ZlangOperatorFunction *oper_MINUS_func */
		NULL , /* ZlangOperatorFunction *oper_MUL_func */
		NULL , /* ZlangOperatorFunction *oper_DIV_func */
		NULL , /* ZlangOperatorFunction *oper_MOD_func */
		
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NEGATIVE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NOT_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_BIT_REVERSE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_PLUS_PLUS_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_MINUS_MINUS_func */
		
		NULL , /* ZlangCompareFunction *comp_EGUAL_func */
		NULL , /* ZlangCompareFunction *comp_NOTEGUAL_func */
		NULL , /* ZlangCompareFunction *comp_LT_func */
		NULL , /* ZlangCompareFunction *comp_LE_func */
		NULL , /* ZlangCompareFunction *comp_GT_func */
		NULL , /* ZlangCompareFunction *comp_GE_func */
		
		NULL , /* ZlangLogicFunction *logic_AND_func */
		NULL , /* ZlangLogicFunction *logic_OR_func */
		
		NULL , /* ZlangLogicFunction *bit_AND_func */
		NULL , /* ZlangLogicFunction *bit_XOR_func */
		NULL , /* ZlangLogicFunction *bit_OR_func */
		NULL , /* ZlangLogicFunction *bit_MOVELEFT_func */
		NULL , /* ZlangLogicFunction *bit_MOVERIGHT_func */
		
		ZlangSummarizeDirectPropertySize_guiclipboard , /* ZlangSummarizeDirectPropertySizeFunction *summarize_direct_prop_size_func */
	} ;

ZlangImportObjectFunction ZlangImportObject_guiclipboard;
struct ZlangObject *ZlangImportObject_guiclipboard( struct ZlangRuntime *rt )
{
	struct ZlangObject	*obj = NULL ;
	struct ZlangFunction	*func = NULL ;
	int			nret = 0 ;
	
	nret = ImportObject( rt , & obj , ZLANG_OBJECT_guiclipboard , & direct_funcs_guiclipboard , sizeof(struct ZlangDirectFunctions) , NULL ) ;
	if( nret )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_LINK_FUNC_TO_ENTITY , "import object to global objects heap" )
		return NULL;
	}
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiclipboard_CopyText_string , ZLANG_OBJECT_void , "CopyText" , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiclipboard_PasteText , ZLANG_OBJECT_string , "PasteText" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	return obj ;
}

